layui.use(['layer','form','table','element','layedit','upload'],function () {
    var layer = layui.layer
        ,form = layui.form
        ,table =layui.table
        ,element = layui.element
        ,layedit = layui.layedit
        ,upload = layui.upload;

    //动态加载type和label
    $.ajax({
        type:"GET",
        url:"/admin/project/getLabelAndType",
        success:function (result) {
            var typeList = result.data.labelTypeList.typeList;
            $.each(typeList,function (i,type) {
                var typeName = type.typeName;
                var id = type.id;
                //过滤免费项目无关的type
                if(typeName != "首页" && typeName != "包赔项目" && typeName != "投资赚钱" && typeName != "靠谱项目" && typeName !="最新项目"){
                    $("#types").append("<input type='radio'  name='type' title='"+typeName+"' value='"+id+"'></input>");
                }
            });

            form.render();
        },
        error:function () {
            layer.msg("系统错误！");
        }
    });


    //普通图片上传
    var uploadInst = upload.render({
        elem: '#stepImg1Btn'
        ,url: '/admin/submitArt/uploadImg'
        ,before: function(obj){
            //预读本地文件示例，不支持ie8
            obj.preview(function(index, file, result){
                $('#stepImg1Demo').attr('src', result); //图片链接（base64）
            });
        }
        ,done: function(res){
            //如果上传失败
            console.log(res);
            var flag = res.code;
            var message = res.msg;
            if(flag==-1){
                layer.msg(message);
            }else{
                //上传成功,则返回url
                console.log(11)
                console.log(res.data.src)
                layer.msg("封面上传成功!");
                $("#stepImg1").attr("value",res.data.src);
            }
        }
        ,error: function(){
            //演示失败状态，并实现重传
            layer.msg("上传失败，请稍候再试！");
        }
    });

    //普通图片上传
    var uploadInst = upload.render({
        elem: '#stepImg2Btn'
        ,url: '/admin/submitArt/uploadImg'
        ,before: function(obj){
            //预读本地文件示例，不支持ie8
            obj.preview(function(index, file, result){
                $('#stepImg2Demo').attr('src', result); //图片链接（base64）
            });
        }
        ,done: function(res){
            //如果上传失败
            console.log(res);
            var flag = res.code;
            var message = res.msg;
            if(flag==-1){
                layer.msg(message);
            }else{
                //上传成功,则返回url
                layer.msg("封面上传成功!");
                $("#stepImg2").attr("value",res.data.src);
            }
        }
        ,error: function(){
            //演示失败状态，并实现重传
            layer.msg("上传失败，请稍候再试！");
        }
    });

    //普通图片上传
    var uploadInst = upload.render({
        elem: '#stepImg3Btn'
        ,url: '/admin/submitArt/uploadImg'
        ,before: function(obj){
            //预读本地文件示例，不支持ie8
            obj.preview(function(index, file, result){
                $('#stepImg3Demo').attr('src', result); //图片链接（base64）
            });
        }
        ,done: function(res){
            //如果上传失败
            console.log(res);
            var flag = res.code;
            var message = res.msg;
            if(flag==-1){
                layer.msg(message);
            }else{
                //上传成功,则返回url
                layer.msg("封面上传成功!");
                $("#stepImg3").attr("value",res.data.src);
            }
        }
        ,error: function(){
            //演示失败状态，并实现重传
            layer.msg("上传失败，请稍候再试！");
        }
    });

    //普通图片上传
    var uploadInst = upload.render({
        elem: '#stepImg4Btn'
        ,url: '/admin/submitArt/uploadImg'
        ,before: function(obj){
            //预读本地文件示例，不支持ie8
            obj.preview(function(index, file, result){
                $('#stepImg4Demo').attr('src', result); //图片链接（base64）
            });
        }
        ,done: function(res){
            //如果上传失败
            console.log(res);
            var flag = res.code;
            var message = res.msg;
            if(flag==-1){
                layer.msg(message);
            }else{
                //上传成功,则返回url
                layer.msg("封面上传成功!");
                $("#stepImg4").attr("value",res.data.src);
            }
        }
        ,error: function(){
            //演示失败状态，并实现重传
            layer.msg("上传失败，请稍候再试！");
        }
    });

    //普通图片上传
    var uploadInst = upload.render({
        elem: '#stepImg5Btn'
        ,url: '/admin/submitArt/uploadImg'
        ,before: function(obj){
            //预读本地文件示例，不支持ie8
            obj.preview(function(index, file, result){
                $('#stepImg5Demo').attr('src', result); //图片链接（base64）
            });
        }
        ,done: function(res){
            //如果上传失败
            console.log(res);
            var flag = res.code;
            var message = res.msg;
            if(flag==-1){
                layer.msg(message);
            }else{
                //上传成功,则返回url
                layer.msg("封面上传成功!");
                $("#stepImg5").attr("value",res.data.src);
            }
        }
        ,error: function(){
            //演示失败状态，并实现重传
            layer.msg("上传失败，请稍候再试！");
        }
    });

    //普通图片上传
    var uploadInst = upload.render({
        elem: '#stepImg6Btn'
        ,url: '/admin/submitArt/uploadImg'
        ,before: function(obj){
            //预读本地文件示例，不支持ie8
            obj.preview(function(index, file, result){
                $('#stepImg6Demo').attr('src', result); //图片链接（base64）
            });
        }
        ,done: function(res){
            //如果上传失败
            console.log(res);
            var flag = res.code;
            var message = res.msg;
            if(flag==-1){
                layer.msg(message);
            }else{
                //上传成功,则返回url
                layer.msg("封面上传成功!");
                $("#stepImg6").attr("value",res.data.src);
            }
        }
        ,error: function(){
            //演示失败状态，并实现重传
            layer.msg("上传失败，请稍候再试！");
        }
    });


    //普通图片上传
    var uploadInst = upload.render({
        elem: '#stepImg7Btn'
        ,url: '/admin/submitArt/uploadImg'
        ,before: function(obj){
            //预读本地文件示例，不支持ie8
            obj.preview(function(index, file, result){
                $('#stepImg7Demo').attr('src', result); //图片链接（base64）
            });
        }
        ,done: function(res){
            //如果上传失败
            console.log(res);
            var flag = res.code;
            var message = res.msg;
            if(flag==-1){
                layer.msg(message);
            }else{
                //上传成功,则返回url
                layer.msg("封面上传成功!");
                $("#stepImg7").attr("value",res.data.src);
            }
        }
        ,error: function(){
            //演示失败状态，并实现重传
            layer.msg("上传失败，请稍候再试！");
        }
    });

    //表单验证
    form.verify({
        title:function (value) {
            if(value == ""){
                return "标题不能为空";
            }
        },
        desc:function (value) {
            if(value == ""){
                return "描述不能为空";
            }
        },
        step1:function (value) {
            if(value == ""){
                return "步骤一不能为空";
            }
        }
    });

    //提交表单
    form.on('submit(form)',function (data) {
        layer.msg('保存中...', { icon: 16, shade: [0.5, '#f5f5f5'], scrollbar: false, offset: '50%', time: 300000 });
        var types = "";
        $("input[name=type]:checked").each(function () {
            types = types + "," + $(this).val();
        });
        types = types.substr(1,types.length-1);
        if(types == ""){
            layer.msg("请选择类型！");
            return;
        }

        if(types != 1){
            var allLength = $("#step1").val().length + $("#step2").val().length + $("#step3").val().length +
                $("#step4").val().length + $("#step5").val().length + $("#step6").val().length + $("#step7").val().length + $("#desc").val().length;
            if(allLength < 200){
                layer.msg("该类型的文章字数需要大于200个字，请尽量描述清楚项目攻略");
                return;
            }
        }
        var paramData = {
            "title":$("#title").val(),
            // "content":layedit.getContent(index),
            // "keyword":$("#title").val(),
            "types":types,
            "summary":$("#desc").val(),
            "step1":$("#step1").val(),
            "stepImg1":$("#stepImg1").val(),
            "step2":$("#step2").val(),
            "stepImg2":$("#stepImg2").val(),
            "step3":$("#step3").val(),
            "stepImg3":$("#stepImg3").val(),
            "step4":$("#step4").val(),
            "stepImg4":$("#stepImg4").val(),
            "step5":$("#step5").val(),
            "stepImg5":$("#stepImg5").val(),
            "step6":$("#step6").val(),
            "stepImg6":$("#stepImg6").val(),
            "step7":$("#step7").val(),
            "stepImg7":$("#stepImg7").val()
        };

        var data2 = JSON.stringify(paramData);

        //保存项目
        $.ajax({
            url:"/admin/submitArt/phoneSave",
            type:"POST",
            data:data2,
            // dataType:"json",
            contentType:"application/json;charset=utf-8",
            success:function (result) {
                if(result.flag){
                    layer.msg(result.message);
                    document.getElementById("form").reset();
                    layui.form.render();
                }else{
                    layer.msg(result.message);
                }
            },
            error:function () {
                layer.msg("系统错误！");
            }
        });

    });


});
